﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Doan_ver1
{
    public partial class NapTien : System.Web.UI.Page
    {
        public string connection()
        {
            return Provider.connectionString();
        }
      

        
        public string generateRandomString()
        {
            string randomString = "";
            int randNumber;
            Random rnd = new Random();

            for (int i = 0; i < 6; i++)
            {
                if (rnd.Next(1, 3) == 1)
                    randNumber = rnd.Next(97, 103); //char {a-z}
                else
                    randNumber = rnd.Next(48, 58); //int {0-9}

                randomString = randomString + (char)randNumber;
            }
            return randomString;
        }
        public void load()
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
            TaiKhoan taiKhoan = data.TaiKhoans.Where(c => c.TenDangNhap == Session["khachhang"] as string).FirstOrDefault();
            Label1.Text = taiKhoan.SoDuTK.ToString().Replace(".000", string.Empty);
            Label3.Text = generateRandomString();
            Session["maxacnhan"] = Label3.Text;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["khachhang"] == null)
                Response.Redirect("DangNhap.aspx");
          //  load();
            if (!IsPostBack)
            {
                //Label3.Text = generateRandomString();
                //Session["maxacnhan"] = Label3.Text;
                load();
            }
            
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
            try
            {

                if (Page.IsValid && TextBox1.Text == Session["maxacnhan"] as string)
                {

                    TaiKhoan taiKhoan = data.TaiKhoans.Where(c => c.TenDangNhap == Session["khachhang"] as string).FirstOrDefault();
                    taiKhoan.SoDuTK = taiKhoan.SoDuTK + Convert.ToDecimal(TextBox2.Text);
                    data.SubmitChanges();

                    ChiTietGD_NH chitiet = new ChiTietGD_NH();
                    List<LoaiGD_NH> list = data.LoaiGD_NHs.ToList();
                    chitiet.LoaiGD_NH = data.LoaiGD_NHs.Where(c => c.MaLoaiGD_NH == "1").First();
                    chitiet.MaTaiKhoan = taiKhoan.MaTaiKhoan;
                    chitiet.SoTien = Convert.ToDecimal(TextBox2.Text);
                    chitiet.Ngay = DateTime.Now;
                    data.ChiTietGD_NHs.InsertOnSubmit(chitiet);
                    data.SubmitChanges();

                    LichSuGD_NH lichSu = new LichSuGD_NH();
                    lichSu.MaChiTietGD_NH = chitiet.MaChiTietGD_NH;
                    lichSu.MaTaiKhoan = taiKhoan.MaTaiKhoan;
                    data.LichSuGD_NHs.InsertOnSubmit(lichSu);
                    data.SubmitChanges();

                    Label2.Text = "Nạp tiền thành công";
                    load();
                    TextBox1.Text = null;
                    TextBox2.Text = null;
                }
                else
                {
                    Label2.Text = "Thất bại, kiểm tra lại thông tin";
                    load();
                    //Label3.Text = generateRandomString();
                    //Session["maxacnhan"] = Label3.Text;
                }
                
            }
            catch( Exception ex)
            {
                throw new Exception("ERROR");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //Label3.Text = generateRandomString();
            load();
        }
    }
}